import { Injectable } from '@angular/core';
import { HttpClient, HttpParams } from '@angular/common/http';
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';

import { ApiService, JsonResult } from './api.service';
import { PageVo, BaselineDetailVo, BaseListVo, BaseListsVo, AddVo } from 'src/app/shared/models';

@Injectable({
  providedIn: 'root'
})
export class BaselineService {

  constructor(
    private apiService: ApiService,
  ) { }

  getBaselinePage(pageIndex: number = 1, pageSize: number = 10): Observable<PageVo<BaseListVo>> {
    console.log('执行方法');
    const params = new HttpParams()
      .append('pageIndex', `${pageIndex}`)
      .append('pageSize', `${pageSize}`);
    return this.apiService.get('/baselines', { params })
      .pipe(
        map((jsonResult: JsonResult<PageVo<BaseListVo>>) => jsonResult.data)
      );
  }

  getBaselineDetailPage(serverId: string, containerId: string): Observable<PageVo<BaselineDetailVo>> {
    console.log('执行方法getBaselineDetailPage');
    const params = new HttpParams()
      .append('serverId', `${serverId}`)
      .append('containerId', `${containerId}`);
    return this.apiService.get('/baseline-detail', { params })
      .pipe(
        map((jsonResult: JsonResult<PageVo<BaselineDetailVo>>) => jsonResult.data)
      );
  }

  getBaselinePageByProjectId(projectId: string, pageIndex: number = 1, pageSize: number = 10): Observable<PageVo<BaseListVo>> {
    const params = new HttpParams()
      .append('projectId', `${projectId}`)
      .append('pageIndex', `${pageIndex}`)
      .append('pageSize', `${pageSize}`);
    return this.apiService.get('/application/baselines', { params })
      .pipe(
        map((jsonResult: JsonResult<PageVo<BaseListVo>>) => jsonResult.data)
      );
  }


  getBaselineList(containerUuid: string): Observable<PageVo<BaseListsVo>> {
    console.log('===getBaselineList===', containerUuid);
    return this.apiService.get('/data/container/' + containerUuid)
      .pipe(
        map((jsonResult: JsonResult<PageVo<BaseListsVo>>) => jsonResult.data)
      );
  }

  addBaseline(resData: any) {
    console.log('== resData ==', resData);
    return this.apiService.post('/data/baseline/singleadd', resData).pipe(
      map((jsonResult: JsonResult<AddVo>) => jsonResult)
    );
  }

  deleteBaseline(name: string, number: string) {
    const params = new HttpParams()
      .append('name', `${name}`)
      .append('baselineNumber', `${number}`);
    return this.apiService.get('/data/baseline/delete', { params })
      .pipe(
        map((jsonResult: JsonResult<AddVo>) => jsonResult.data)
      );
  }

}
